//// 进度条

function LoadingBar( ) {
    var self = this;
    var width = 0;
    var height =0;
    var container = null;
    var shape = null;
    var colorBorder = "#000000";
    var colorFill = "rgba(1,1,1,0.5)";
    var fontSize = "30";
    var fontColor = "#000000";
    var txt = null;

    var size = {x:0,y:0,width:0,height:0};

    self.InitComponet = function ($width,$height,$colorBorder,$colorFill,$fontSize,$fontColor) {

        width = $width;
        height= $height;
        colorBorder =$colorBorder;
        colorFill = $colorFill;
        fontSize = $fontSize+"px Arial";
        fontColor = $fontColor;

        container = new createjs.Container();
        shape = new createjs.Shape();
        shape.graphics.setStrokeStyle(2);
        shape.graphics.beginStroke(colorBorder);
        shape.graphics.drawRect (0,0,width,height);

        txt = new createjs.Text( "Loading...", fontSize, fontColor );
        //txt.width = width;
        // txt.textAlign = "center";
        // txt.regX = txt.getMeasuredWidth() * 0.5;
        txt.x = (width-txt.getMeasuredWidth()) * 0.5;
        txt.y = 0;

        var txtHeight = txt.getMeasuredHeight();
        shape.x = 0;
        shape.y = txtHeight * 1.5;
        container.addChild(txt,shape);
        return container;
    };

    self.getSize =function () {
        size.x = 0;
        size.y = 0;
        size.width = width;
        size.height= width + shape.y;
        return size;

    };

    self.updateProgress = function ($value) {
        if(shape!==null){
            shape.graphics.clear ();
            shape.graphics.setStrokeStyle(1);
            shape.graphics.beginStroke(colorBorder);
            shape.graphics.drawRect (0,0,width,height);
            shape.graphics.beginFill(fontColor);
            shape.graphics.drawRect (0,0,width*$value,height);
        }
        if(txt!==null){
            txt.text = "Loading."+Math.round($value*100)+"%";
            txt.x = (width-txt.getMeasuredWidth()) * 0.5;
            txt.y = 0;
        }
    };

    self.remove = function () {
        if(container!=null){
            if(container.parent != null ){
                container.parent.removeChild(container);
            }
        }
    }
}
//// 能量环
function CirclePower(){
    var self = this;
    var ringShape = null;
    var colorArr = new Array(0x00a550,0x36b558,0x5fbd65,0x76c36e,0x83c671,0x98ce7f,0xcfe183,0xddd982,0xedd37e,0xf8c785,0xfab382,0xf8a885,0xf05151,0xef3d3a,0xee2a27);
    var _num = null;
    var _x = null;
    var _y = null;
    var _radius = null;

    var _times = null;
    var _timeCount = null;
    var _timeTotal = null;

    var _timer = null;

    self.OnTimeComplete = null;

    self.InitComponet = function ( num, x,y,radius){
        _num = num;
        _x = x;
        _y = y;
        _radius = radius;
        ringShape = new createjs.Shape().set({x:_x, y:_y});
        ringShape.shadow = new createjs.Shadow("#B1820C",0,0,1 );
        ringShape.rotation = -90;
        return ringShape;
    };

    self.startMission = function ($time,$count) {
        _times = $time/$count;
        _timeCount = $count;
        _timeTotal = $count;
        _timer = window.setTimeout(onHeartBeatMainHandle,_times);
    };

    self.removeChild = function () {
        if(_timer != null ){
            window.clearTimeout(_timer);
            _timer = null;
        }
        if(ringShape!= null && ringShape.parent != null){
            ringShape.parent.removeChild(ringShape);
        }
    }

    function onHeartBeatMainHandle() {
        window.clearTimeout(_timer);
        _timeCount--;
        if(_timeCount>0){
            _timer = window.setTimeout(onHeartBeatMainHandle,_times);
            self.UpdateProcess((_timeTotal-_timeCount)/_timeTotal);
        }else{
            _timer = null;
            if(self.OnTimeComplete!= null)
                self.OnTimeComplete();
        }
    }

    self.UpdateProcess = function (process) {
        if(ringShape == null) return;

        ringShape.graphics.clear();
        ringShape.graphics.setStrokeStyle(10.0);
        var space = 2*Math.PI / _num;
        var angle1 = space * 0.7;
        var angle2 = space * 0.3;
        var tempIndex = 0;
        var startAngle = 0;
        var endAngle = 0;
        var rgbColor = 0x00a550;

        var colorArrLen = colorArr.length;
        var colorArrTemp = new Array(_num);

        var keyIndex =  Math.floor(process*_num);
        //console.log(keyIndex);
        var colorIndex = Math.floor( colorArrLen * 0.5);
        colorIndex = keyIndex - colorIndex;
        if( colorIndex > 0 ) colorIndex *= -1;
        //console.log(colorIndex);
        var tempColorIndex = 0;
        for( tempIndex =0; tempIndex < _num; tempIndex++ )
        {
            if( colorIndex > 0 ){
                if(tempColorIndex<colorArrLen){
                    colorArrTemp[tempIndex] = colorArr[tempColorIndex];
                    tempColorIndex++;
                }else{
                    colorArrTemp[tempIndex] = colorArr[colorArrLen-1];
                }
            }else{
                colorArrTemp[tempIndex] = colorArr[0];
            }
            colorIndex++;
        }
        //  console.log(colorArrTemp);

        for( tempIndex =0; tempIndex < _num; tempIndex++ )
        {
            startAngle = tempIndex * space;
            endAngle = startAngle + angle1;
            ringShape.graphics.beginStroke(createjs.Graphics.getRGB(colorArrTemp[_num-1-tempIndex], 1));
            ringShape.graphics.arc(0,0,_radius,startAngle,endAngle);
            ringShape.graphics.endStroke();
            ringShape.graphics.beginStroke("rgba(0,0,0,0)");
            startAngle = endAngle;
            endAngle = endAngle + angle2;
            ringShape.graphics.arc(0,0,_radius,startAngle,endAngle);
            ringShape.graphics.endStroke();
        }

        ringShape.cache(-_radius*1.1,-_radius*1.1,_radius*2*1.1,_radius*2*1.1);
    }
}

//// 计时器
function TimerClock(){
    var self = this;
    var container = null;
    var assetsArr = [];
    var index = 0;
    var time = 0;
    var backupScaleX = 1;
    var backupScaleY = 1;

    self.OnTimeComplete = null;

    self.InitComponet = function( $assetsArr, $time ){
        assetsArr = $assetsArr;
        time = $time;
        index = assetsArr.length-1;
        container = new createjs.Container();
        return container;
    };

    self.startTimer = function () {
        backupScaleX = assetsArr[index].scaleX;
        backupScaleY = assetsArr[index].scaleY;

        assetsArr[index].scaleX = 0.01;
        assetsArr[index].scaleY = 0.01;
        assetsArr[index].alpha = 0.2;

        container.addChild(assetsArr[index]);
        createjs.Tween.get(assetsArr[index]).to({scaleX:backupScaleX,alpha:1,scaleY:backupScaleY}, time).call(tweenComplete);
    };

    self.remove = function () {
        if(container!=null){
            if(container.parent != null ){
                container.parent.removeChild(container);
            }
        }
    };

    function tweenComplete() {
        var tempRemove = assetsArr[index];
        var tempIndex = index;
        createjs.Tween.get(tempRemove).to({alpha:0}, 100).call(function () {
            tempRemove.parent.removeChild(tempRemove);
            if(tempIndex==0){
                if(self.OnTimeComplete != null){
                    self.OnTimeComplete();
                }
            }
        });
        index--;
        if(index>=0){
            self.startTimer();
        }
    }

}

function GetNumPicture($bitmapArr, $value, $width ) {
    var container = new createjs.Container();
    var txt = $value + "";
    var j = 0;
    var currentx = 0;
    for(var i = 0; i < txt.length; i++){
        j = parseInt(txt.charAt(i));
        var t = $bitmapArr[j].clone();
        t.x = currentx;
        container.addChild(t);
        currentx += $width;
    }
    return container;
}

//// 获得角度值
function GetAngle( $x, $y ){
    var angle = 0;
    if( $x === 0 ){
        angle = $y > 0 ? Math.PI*0.5 : Math.PI*1.5;
    }else{
        if( $x >= 0 )
            angle = Math.atan( $y / $x );
        else {
            angle = Math.PI + Math.atan( $y / $x );
        }
    }
    angle*= (180/Math.PI);
    return angle;
}

//// 获得Img像素数据
function TempCanvas( texture ) {
    var tCanvas = document.createElement('canvas');
    var imageWidth = texture.width;
    var imageHeight =texture.height;
    tCanvas.width = imageWidth;
    tCanvas.height = imageHeight;
    var ctx = tCanvas.getContext("2d");
    ctx.drawImage(texture, 0, 0, imageWidth,imageHeight);
    return ctx.getImageData(0, 0, imageWidth, imageHeight).data;
}

///// 分裂特效
function ImageEffect_split() {
    var self = this;
    var container;
    var split_num_x;
    var split_num_y;
    var srcImage;
    var spriteArr = [];
    var spriteTimeArr = [];
    var spriteDelayTimeArr = [];
    var spriteStartXTimeArr=[];
    var spriteStartYTimeArr=[];

    var spritesContainer;
    var splitIndexs = [];
    var resultSprite;
    self.OnShowed = null;
    self.width = 0;
    self.height = 0;
    self.InitComponet = function ($srcImage,$splitX,$splitY ) {

        ccTime = 0;

        srcImage = $srcImage;
        split_num_x = $splitX;
        split_num_y = $splitY;
        container = new createjs.Container();
        spritesContainer = new createjs.Container();
        var imageWidth = $srcImage.width;
        var imageHeight =$srcImage.height;
        self.width = imageWidth;
        self.height =imageHeight;
        resultSprite = new createjs.Shape(new createjs.Graphics().beginBitmapFill(srcImage).drawRect(0, 0, imageWidth, imageHeight));
        resultSprite.alpha = 0;
        container.addChild(spritesContainer,resultSprite);
        var pixelData = TempCanvas(srcImage);
        var stepWidth = imageWidth / split_num_x;
        var stepHeight= imageHeight / split_num_y;
        var tempX = 0;
        var tempY = 0;
        var random = 0;
        var i,j;
        for( j = 0; j < split_num_y+1; j++)
        {
            var arr = [];
            for ( i = 0; i < split_num_x+1; i++)
            {
                tempX = i*stepWidth;
                tempY = j * stepHeight;

                if(i === split_num_x)
                {
                    tempX = imageWidth;
                }
                else if(j === split_num_y)
                {
                    tempY = imageHeight;
                }
                else if(i !== 0 && j !== 0)
                {
                    random = Math.random() - 0.5;
                    tempX += (stepWidth * random*0.85);
                    random = Math.random() - 0.5;
                    tempY += (stepHeight * random*0.85);
                }

                arr.push( [tempX,tempY] );
            }
            splitIndexs.push(arr);
        }

        var x0,x1,x2,x3,y0,y1,y2,y3;
        var value0,value1,value2,value3;
        var targetIndex = 0;
        var rect0,rect1;

        for (i = 0; i <split_num_x-1; i++)
        {
            for( j = 0; j <split_num_y-1; j++)
            {
                targetIndex = j * split_num_y + i;

                var sp0 = new createjs.Shape();
                var sp1 = new createjs.Shape();
                sp0.graphics.beginBitmapFill( srcImage );
                sp1.graphics.beginBitmapFill( srcImage );
                // sp0.graphics.setStrokeStyle(0);
                // sp1.graphics.setStrokeStyle(0);
                //  sp0.graphics.beginStroke("#ff0000");
                //  sp1.graphics.beginStroke("#000000");
                x0 = Math.floor(splitIndexs[j][i][0]);
                y0 = Math.floor(splitIndexs[j][i][1]);
                x1 = Math.floor(splitIndexs[j+1][i][0]);
                y1 = Math.floor(splitIndexs[j+1][i][1]);
                x2 = Math.floor(splitIndexs[j+1][i+1][0]);
                y2 = Math.floor(splitIndexs[j+1][i+1][1]);
                x3 = Math.floor(splitIndexs[j][i+1][0]);
                y3 = Math.floor(splitIndexs[j][i+1][1]);

                value0 = pixelData[(y0*imageWidth+x0-1)*4+3];
                value1 = pixelData[(y1*imageWidth+x1-1)*4+3];
                value2 = pixelData[(y2*imageWidth+x2-1)*4+3];
                value3 = pixelData[(y3*imageWidth+x3-1)*4+3];

                if(value0===0&&value1===0&&value2===0&&value3===0)  continue;

                sp0.graphics.moveTo(x0, y0).lineTo(x1, y1).lineTo(x2, y2).lineTo(x0, y0);
                sp1.graphics.moveTo(x0, y0).lineTo(x2, y2).lineTo(x3, y3).lineTo(x0, y0);
                rect0 = getRect(x0,x1,x2,y0,y1,y2);
                rect1 = getRect(x0,x2,x3,y0,y2,y3);

                // console.log(rect0);
                //sp1.graphics.drawRect(rect0.x, rect0.y, rect0.width, rect0.height);

                sp0.alpha = 0;
                sp1.alpha = 0;
                random = Math.random();
                sp0.x = -imageWidth;///-imageWidth * random;
                random = Math.random()-0.5;
                sp0.y =   imageHeight * random;

                random = Math.random();
                sp1.x = -imageWidth * random*1.5;
                random = Math.random()-0.5;
                sp1.y = imageHeight * random*0.8;

                spriteArr.push(sp0,sp1);
                spritesContainer.addChild(sp0,sp1);

                //////spriteTimeArr.push(65*(split_num_x-i)+1200*Math.random(),65*(split_num_x-i)+1100*Math.random());
                spriteStartXTimeArr.push(sp0.x,sp1.x);
                spriteStartYTimeArr.push(sp0.y,sp1.y);
                if(i < split_num_x*0.3)
                {
                    spriteTimeArr.push(4*(split_num_x-i+split_num_x*0.3)+8*(i-split_num_x*0.3)+600*Math.random(),4*(split_num_x-i+split_num_x*0.3)+4*(i-split_num_x*0.3)+600*Math.random());
                }
                else
                {
                    spriteTimeArr.push(55*(split_num_x-i)+900*Math.random(),55*(split_num_x-i)+800*Math.random());
                }
                spriteDelayTimeArr.push(2.5*i,2.5*i);
                sp0.snapToPixel = true;
                sp1.snapToPixel = true;
                sp0.cache(rect0.x, rect0.y, rect0.width, rect0.height);
                sp1.cache(rect1.x, rect1.y, rect1.width, rect1.height);
            }
        }
        return container;
    };

    function getRect(x0,x1,x2,y0,y1,y2) {
        var maxX = Math.max(x0,x1,x2);
        var minX = Math.min(x0,x1,x2);
        var maxY = Math.max(y0,y1,y2);
        var minY = Math.min(y0,y1,y2);
        return new createjs.Rectangle(minX, minY, maxX-minX, maxY-minY);
    }


    var ccTime = 0;
    function handleTick(event) {

        ccTime += event.delta;
        var isEnd = true;
        var amount = 0;
        for( var j = spriteArr.length-1; j >=0; j--)
        {
            amount = (ccTime-spriteDelayTimeArr[j]) /spriteTimeArr[j];

            if(amount <= 0) continue;

            if(ccTime>=spriteDelayTimeArr[j] && amount < 1)
            {
                isEnd = false;
                spriteArr[j].x = (0 - spriteStartXTimeArr[j]) * amount + spriteStartXTimeArr[j];
                spriteArr[j].y = (0 - spriteStartYTimeArr[j]) * amount + spriteStartYTimeArr[j];
                spriteArr[j].alpha = amount;
            }

            if((1-amount)<1/event.delta)
            {
                spriteArr[j].x = 0;
                spriteArr[j].y = 0;
                spriteArr[j].alpha = 1;
            }
        }
        if(isEnd)
        {
            createjs.Ticker.removeEventListener("tick", handleTick);
            createjs.Tween.get(resultSprite).to({alpha: 1}, 300).call(
                function () {
                    if(self.OnShowed!==null)
                    {
                        container.removeChild(spritesContainer);
                        spritesContainer = null;
                        while(spriteArr.length!==0){
                            spriteArr.pop();
                        }
                        spriteArr = null;
                        self.OnShowed();
                    }
                }
            );
        }
    }
    self.showMe = function () {
        createjs.Ticker.addEventListener("tick", handleTick);
        // var j,random;
        // for( j = spriteArr.length-2; j >=0; j--)
        // {
        //     random = Math.random()-0.35;
        //     createjs.Tween.get(spriteArr[j]).to({x: 0, y:0,  alpha: 1}, j+1200*random);
        // }
        //
        // createjs.Tween.get(spriteArr[spriteArr.length-1]).to({x: 0, y:0,  alpha: 1}, (spriteArr.length-1)+500).call(
        //     function () {
        //         createjs.Tween.get(resultSprite).to({alpha: 1}, 750).call(
        //             function () {
        //                 if(self.OnShowed!==null)
        //                 {
        //                     container.removeChild(spritesContainer);
        //                     spritesContainer = null;
        //                     while(spriteArr.length!==0){
        //                         spriteArr.pop();
        //                     }
        //                     spriteArr = null;
        //                     self.OnShowed();
        //                 }
        //             }
        //         );
        //     }
        // );
    }
}



//// 梯度渐现 | 隐藏
function ImageEffect_gradient_in( $spriteTarget, $spriteWidth, $spriteHeight, $amountHeight, $time, $stage ) {
    //  var sWidth = $spriteWidth;
    //  var sHeight = $spriteHeight;
    //  var amount = 0.15;//$amountHeight;
    //  var maskShape = new createjs.Shape();
    //  var blankColor = "rgba(0, 0, 0, 0)";
    //  var fullColor = "#000000";
    //  maskShape.graphics.beginFill(fullColor);
    //  maskShape.graphics.drawRect(0,0,sWidth*0.5,sHeight*amount);
    //  maskShape.graphics.beginLinearGradientFill([fullColor,blankColor], [0, 1], 0, sHeight*(1-amount), 0, sHeight).drawRect(0, sHeight*(1-amount), sWidth*0.5, sHeight*amount);
    // // $stage.addChild(maskShape);
    //  $spriteTarget.filters = [new createjs.AlphaMaskFilter(maskShape.cacheCanvas)];
    //  $spriteTarget.cache(0, 0,  sWidth, sHeight);

    var box = new createjs.Shape();
    box.graphics.beginLinearGradientFill(["#000000", "rgba(0, 0, 0, 0)"], [0, 1], 0, 0, 0, 200)
    box.graphics.drawRect(0, 0, 200, 200);
    box.cache(0, 0, 200, 200);
    $spriteTarget.filters = [
        new createjs.AlphaMaskFilter(box.cacheCanvas)
    ];
    $spriteTarget.cache(0, 0, 200, 200);


    //$spriteTarget.updateCache(0, 0, sWidth, sHeight*amount);

}


